<?php

class CouponCodeVerificationService extends PaymentData{
	
	private $_verifyMethod;
	private $_customerData;
	private $_couponCodeData=null;
	private $_couponCodeErrorMessage;
	private $_db;
	private $_limitArray;
	private $_categorySql;
	private $_bidType;
	public $code;
	private $_allowScenes;
	private $_price;
	private $_couponCodeId;
	
	public function __construct(){
		$this->_db = Yii::app()->db;
		$date = time();
		$CustomerModel = new Customer();
		$CustomerData1 = $CustomerModel->find("t.id =".Yii::app()->user->id);
		$customerdata	= $CustomerData1->attributes;
		// $customerdata = Yii::app()->user->userInfo()->attributes;
		$this->setCustomerData($customerdata);
		$this->_verifyMethod = array(
				'allowExists'		=>  array('functions'=>'allowExists','params'=>''),
				'allowIsActive'		=>  array('functions'=>'allowIsActive','params'=>''),
				'allowPeriodOfUse'  =>	array('functions'=>'allowPeriodOfUse','params'=>''),
// 				'allowNumber' 		=>	array('functions'=>'allowNumber','params'=>''),
 				'allowNumberOfUsers'	=>	array('functions'=>'allowNumberOfUsers','params'=>''),
				'allowEveryUsed' 	=> 	array('functions'=>'allowEveryUsed','params'=>''),
// 				'allowNobodyUsed'	=>	array('functions'=>'allowNobodyUsed','params'=>''),
		);
	}
	
	public function setCouponCode($couponCode){
		$this->code = $couponCode;
		return $this;
	}
	
	public function toArray(){
		return array(
			'code'	=> $this->code,	
		);
	}
	
	/**
	 * Set Customer Info
	 * @param unknown_type $customerdata
	 */
	public function setCustomerData($customerdata){
		$this->_customerData = $customerdata;
		return $this;
	}
	
	public function setCouponCodeData(){
		$data = CouponCode::model()->find('coupon_code = "'.$this->code.'"');
		if(!empty($data)){
			$this->_couponCodeData = $data->attributes;
			$this->_couponCodeId = $this->_couponCodeData['id'];
            //auto responder coupon code verification
            if($this->_couponCodeData['type'] == '2')    $this->setVerifyMethod(array('allowResponder'=>array('functions'=>'allowResponder','params'=>'')));
		}
		return $this;
	}

	public function getCouponCodeData(){
		return $this->_couponCodeData;
	}
	
	public function setPrice($price){
		$this->_price = $price;
		return $this;
	}
	
	public function setCouponCodeId($couponCodeId){
		$this->_couponCodeId = $couponCodeId;
		return $this;
	}
	
	public function getCouponCodeId(){
		return $this->_couponCodeId;
	}
	
	/**
	 * Add Verify Method
	 * @param array $verifyMethod
	 * @return BidVerificationService
	 */
	public function setVerifyMethod(array $verifyMethod){
		foreach ($verifyMethod as $key => $val){
			$this->_verifyMethod[$key] = $val;
		}
		return $this;
	}
	
	/**
	 * Get Error Message
	 */
	public function getErrorMessage(){
		return $this->_couponCodeErrorMessage;
	}
	
	public function allowNumber(){
		if($this->_couponCodeData['last_number']){
			return true;
		}else{
			$this->_couponCodeErrorMessage = 'Invalid: Frequency Of Use Of The Code Has Run Out.';
			return false;
		}
	}
	
	public function allowNumberOfUsers(){
        if($this->_couponCodeData['type'] == '2'){
            if($this->_couponCodeData['user_number']==$this->_couponCodeData['max_number']){
                $this->_couponCodeErrorMessage = 'Invalid: Invalid Coupon Code.';
                return false;
            }
        }
	    return true;
	}
	
	public function allowExists(){
		if(empty($this->_couponCodeData)){
			$this->_couponCodeErrorMessage = 'Invalid: This Coupon Code Does Not Exist.';
			return false;
		}else{
			return true;
		}
	}
	
	public function allowIsActive(){
		if($this->_couponCodeData['active']){
			return true;
		}else{
			$this->_couponCodeErrorMessage = 'Invalid: This Coupon Code Does Not Exist.';
			return false;
		}
	}
	
	public function allowPeriodOfUse(){
		if($this->_couponCodeData['start_time']>time()){
			$this->_couponCodeErrorMessage = 'Invalid: This Coupon Code Does Not Exist.';
			return false;
		}elseif($this->_couponCodeData['expired_time']<time()){
			$this->_couponCodeErrorMessage = 'Invalid: This Coupon Code Does Not Exist.';
			return false;
		}else{
			return true;
		}
	}
	
	public function allowEveryUsed(){
		$count = CouponCodeLog::model()->count('customer_id = '.$this->_customerData['id'].' and coupon_code_id='.$this->_couponCodeId);
		if($count>=$this->_couponCodeData['max_customer_every_used'] && $this->_couponCodeData['max_customer_every_used']>0){
			$this->_couponCodeErrorMessage = 'Invalid: You have already used this code the maximum number of times!';
			return false;
		}else{
			return true;
		}
	}
	
	public function allowScenes(){
		if($this->_couponCodeData['place_userd_bin']){
			$this->_allowScenes[] = "\"Buy It Now\" / ";
		}elseif($this->_couponCodeData['place_userd_paynow']){
			$this->_allowScenes[] = "\"Pay Now\" / ";
		}elseif($this->_couponCodeData['place_userd_bidpack']){
			$this->_allowScenes[] = "\"Buy Bids\" / ";
		}elseif($this->_couponCodeData['place_userd_prime']){
			$this->_allowScenes[] = "\"Prime User\" / ";
		}elseif($this->_couponCodeData['place_userd_required']){
			$this->_allowScenes[] = "\"My Account\" / ";
		}
		return $this->_allowScenes;
	}

    public function allowResponder(){
        if($this->_couponCodeData['customer_id'] != Yii::app()->user->id  ){
            $this->_couponCodeErrorMessage = 'Invalid: Invalid Coupon Code.';
            return false;
        }
        return true;
    }
	
	public function lessCouponCode(){
// 		CouponCode::model()->updateAll(array('customer_id' => $this->_customerData['id'],'coupon_code="'.$this->code.'"'));
// 		CouponCode::model()->updateCounters(array('last_number'=>-1,'user_number'=>1),'last_number>0 and coupon_code="'.$this->code.'"');
		CouponCode::model()->updateCounters(array('user_number'=>1),'coupon_code="'.$this->code.'"');
	}
	
	public function addCouponCode(){
		CouponCode::model()->updateCounters(array('last_number'=>1,'user_number'=>-1),'user_number>0 and coupon_code="'.$this->code.'"');
	}
	
	public function disconutAmountCouponCode(){
		return $this->_price-$this->_couponCodeData['disconut_amount'];
	}
	
	public function disconutPercentageCouponCode(){
		return $this->_price-( $this->_price*$this->_couponCodeData['disconut_amount']);
	}
	
	public function useCouponCode(){
		if($this->_couponCodeData['discount_type']==1){
			return $this->disconutAmountCouponCode();
		}elseif($this->_couponCodeData['discount_type']==2){
			return $this->disconutPercentageCouponCode();
		}else{
			return $this->_price;
		}
	}
	
	/**
	 * Perform Authentication Method
	 */
	public function verification(){
	
		foreach ($this->_verifyMethod as $key => $val){
			if(method_exists($this,$val['functions'])){
				$return = $this->$val['functions']($val['params']);
				if(!$return){
					return false;
				}
			}
		}
		return true;
	}

}
